<!DOCTYPE html>
<html>
<head>
    <title>订单</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <link rel="stylesheet" href="../lib/weui.min.css">
    <link rel="stylesheet" href="../css/jquery-weui.css">
    <link rel="stylesheet" href="../css/base.css?13">
    <link rel="stylesheet" href="../css/addDetail.css?13">
</head>
<body>
<div class="xyg-add-detail-address weui-cells">
    <a class="weui-cell weui-cell_access" onclick="goAddressAndType()">
        <div class="weui-cell__bd" id="addressInfo">
            请选择地址
            <script type="text/x-handlebars-template" id="address-temp">
                <div class="xyg-add-detail-text">
                    {{name}}<span>{{phone}}</span>
                </div>
                <div class="xyg-add-detail-desc">
                    {{province}}{{descripts}}
                </div>
            </script>
        </div>
        <div class="weui-cell__ft"></div>
    </a>
    <div style="width:100%; height:12px; background:url(../images/line_icon.png) repeat;"></div>
</div>
<div class="xyg-cart">
    <div class="xyg-cart-show">
        <div class="xyg-cart-list">
            <div class="xyg-cart-list-head">
                <div class="xyg-cart-list-head-title">
                    自营
                </div>
            </div>
            <div id="itemList">
                <script type="text/x-handlebars-template" id="item-list-temp">
                    <div class="weui-cell weui-cell_swiped">
                        <div class="xyg-cart-list-content weui-cell__bd">
                            <div class="xyg-cart-list-content-pic">
                                <img src="{{image}}" alt="">
                            </div>
                            <div class="xyg-cart-list-content-text">
                                <div class="xyg-cart-list-content-text-title">
                                    {{title}}
                                </div>
                                <div class="xyg-cart-list-content-text-proper">
                                    {{paramData}}
                                </div>
                                <div class="xyg-cart-list-content-text-pricenum">
                                    <div class="xyg-cart-list-content-text-price">
                                        ￥{{prePrice}}
                                        <num>X {{num}}</num>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </script>
            </div>
        </div>
    </div>
</div>
<div class="xyg-add-detail-pay-text weui-cells">
    <a class="weui-cell weui-cell_access">
        <div class="weui-cell__bd">
            <p>商品总价：</p>
        </div>
        <div id="sumPrice"></div>
    </a>

    <a class="weui-cell weui-cell_access">
        <div class="weui-cell__bd">
            <p>购买时间：</p>
        </div>
        <div id="payTime"></div>
    </a>

    <!--<a class="weui-cell weui-cell_access" href="javascript:showParams();">
        <div class="weui-cell__bd">
            <p>优惠券：</p>
        </div>
        <div class="weui-cell__ft textfont" id="couponShow">1张可用</div>
    </a>-->

    <a class="weui-cell weui-cell_access">
        <div class="weui-cell__bd">
            <p>支付方式：</p>
        </div>
        <div class="textfont">微信支付</div>
    </a>
</div>
<!--底部开始-->
<div class="xyg-product-footer">
    <div class="xyg-product-footer-tab1">
        <div class="xyg-product-footer-tab-center">
            <div class="xyg-product-footer-tab xyg-product-footer-tab1-pic">
                <input type="hidden" id="payMoney" value="${order.payment}">
                <div class="xyg-product-footer-text xyg-product-footer-text-price" id="payMent">
                    合计：
                </div>
                <div class="xyg-product-footer-text xyg-product-footer-text-btn"
                     onclick="pay()">
                    去支付
                </div>
            </div>
        </div>
    </div>
</div>
<!--底部结束-->
<!--选择开始-->
<div id="xyg-params" class="weui-popup__container popup-bottom">
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
        <div class="xyg-params-content weui-cells_checkbox">
            <img class="xyg-params-content-del" onClick="closePoupp()" src="../images/icon-del-buy.png"/>
            <div class="xyg-coupon-show">
                <!--优惠券开始-->
                <#list couponList as coupon>
                    <div class="xyg-coupon-show-item">
                        <div class="xyg-coupon-show-left">
                            <label class="weui-cell" style="padding:2rem 0px 0px 0px;">
                                <input type="checkbox" class="weui-check" name="coupon" value="${coupon.id}"
                                       data-value="-${coupon.reduceMoney}"><i class="weui-icon-checked"></i>
                            </label>
                        </div>
                        <div class="xyg-coupon-show-right">
                            <div class="xyg-coupon">
                                <img class="xyg-coupon-bg" src="../images/coupon1.jpg?1234"/>
                                <div class="xyg-coupon-content">
                                    <div class="xyg-coupon-content-left">
                                        <span>￥</span>${coupon.reduceMoney}
                                    </div>
                                    <div class="xyg-coupon-content-right">
                                        <div class="xyg-coupon-content-right-content">
                                            <div class="xyg-coupon-content-right-content-request">
                                                满￥${coupon.fullMoney}使用
                                            </div>
                                            <div class="xyg-coupon-content-right-content-type">全场通用</div>
                                            <div class="xyg-coupon-content-right-content-bg">
                                                ${(coupon.startTime?string('yyyy-MM-dd
                                                HH:mm:ss'))!''}~${(coupon.endTime?string('yyyy-MM-dd HH:mm:ss'))!''}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </#list>
                <!--优惠券结束-->
            </div>
            <a href="javascript:closePoupp();" class="weui-btn weui-btn_primary">确认</a>
        </div>
    </div>
</div>
<!--选择结束-->
</body>
<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script src="../lib/jquery-2.1.4.js"></script>
<script src="../js/jquery-weui.js"></script>
<script src="../js/swiper.min.js"></script>
<script src="../fonts/iconfont.js"></script>
<script src="../lib/fastclick.js"></script>
<script src="../js/jquery-ui.min.js"></script>
<script src="../lib/handlebars.min.js"></script>
<script src="../js/config.js"></script>
<script>
    var memberId = getQueryString("memberId");
    var type = getQueryString("type");
    var orderItemJson = getQueryString("orderItemJson");
    var orderId = getQueryString("orderId");

    var addressId = getQueryString("addressId");
    var sumPrice = 0;
    // 解析
    /*var dataObj = eval("("+orderItemJson+")");
    $.each(dataObj, function (index, value) {
        console.log(value)
    });*/
    // 获取默认地址,如果没有不设置
    $.get(baseURL + "api/address/default?memberId="+memberId+"&addressId="+addressId,"",function(res) {
        if (res.code == 0) {
            //var address = res.address;
            var addressTemp = $('#address-temp').html();
            var hanAddress = Handlebars.compile(addressTemp);
            $('#addressInfo').html(hanAddress(res.address));
            addressId = res.address.id;
        }
    });

    // 获取商品信息
    $.get(baseURL + "api/orderItem/info/"+encodeURI(orderItemJson),"",function(res) {
        if (res.code == 0) {
            var orderItemList = res.orderItemList;
            if (orderItemList != null && orderItemList.length > 0) {
                $.each(orderItemList, function (index, value) {
                    // 总价
                    sumPrice+=parseFloat(value.prePrice);
                    var image = value.image;
                    var imageArr = image.split(",");
                    value.image = imageArr[0];
                    var itemTemp = $('#item-list-temp').html();
                    var hanItem = Handlebars.compile(itemTemp);
                    $('#itemList').append(hanItem(value));
                });
                // 总价
                $("#sumPrice").html("￥"+sumPrice);
                $("#payTime").html(new Date().Format("yyyy-MM-dd HH:mm:ss"));
                $("#payMent").html("合计:"+sumPrice);
            }
        }
    });

    function pay() {
        if (addressId == null || addressId == "") {
            $.toast("请选择收货地址", "text");
            return false;
        }
        var param = {
            memberId: memberId,
            addressId: addressId,
            payment: sumPrice,
            orderItemJson: orderItemJson
        };
        $.confirm("您确定要支付吗？", "提示", function () {
            $.showLoading("正在提交中");
            $.post(baseURL + "api/order/pay", param, function (res) {
                if (res.code == 0) {
                    $.hideLoading();
                    //alert(1);
                    //window.location.href = "/phone/detailList?memberId=" + memberId + "&status=2";
                } else {
                    $.hideLoading();
                    $.toast(res.msg, "text");
                }
            }, "json");
        }, function () {
            //取消操作
        });
    }

    function cancelOrder(orderId, memberId) {
        var param = {orderId: orderId, memberId: memberId}
        $.showLoading("正在提交中");
        $.post("/phone/order/cancel", param, function (result) {
            if (1 == result.returnInfo.state) {
                $.hideLoading();
                //window.location.href = "/phone/detailList?memberId=" + memberId + "&status=1";
                /* window.location.href="/phone/detailList?memberId="+memberId+"&status=2";*/
            } else {
                $.hideLoading();
                $.toast(result.returnInfo.errorMsg, "text");
            }
        }, "json");
    }

    function showBalance(obj) {
        // 账户余额
        var balanceMoney = $(obj).val();
        var memberMoney = $("#memberMoney").val();
        var paymentMoney = $("#paymentMoney").val();
        var couponMoney = 0;
        var couponId = $("input[name='coupon']:checked").val();
        if (couponId != null && couponId != "") {
            couponMoney = $("input[name='coupon']:checked").attr("data-value");
        }
        if ($(obj).is(":checked")) {
            // 获取总金额
            var rpayMoney = parseFloat(paymentMoney) + parseFloat(couponMoney) - parseFloat(memberMoney);
            // 说明余额多了
            if (rpayMoney < 0) {
                $("#balanceMoney").val(parseFloat(paymentMoney) + parseFloat(couponMoney));
                $("#payMent").html("合计：￥0");
                $("#payMoney").val(0);
                return false;
            }
            $("#balanceMoney").val(memberMoney);
            $("#payMoney").val(rpayMoney);
            $("#payMent").html("合计：￥" + rpayMoney);

        } else {
            var rpayMoney = parseFloat(paymentMoney) + parseFloat(couponMoney);
            $("#balanceMoney").val(0);
            $("#payMoney").val(rpayMoney);
            $("#payMent").html("合计：￥" + rpayMoney);
        }
    }


    function showParams() {
        $("#xyg-params").popup();
    }
    ;

    function closePoupp() {
        var paymentMoney = $("#paymentMoney").val();
        var balanceMoney = 0;
        if ($("#balanceMoney").is(":checked")) {
            balanceMoney = $("#balanceMoney").val();
        }
        // 获取优惠券
        var couponIdArr = [];
        var couponCss = "";
        var couponMoney = 0;
        $.each($('input[name=coupon]:checkbox:checked'), function () {
            couponIdArr.push($(this).val());
            couponCss = $(this).attr("data-value");
            couponMoney = $(this).attr("data-value");

        });
        if (couponIdArr == null || couponIdArr.length == 0) {
            // layer.alert("请选择优惠券");
            couponCss = couponSize + "张可用";
            couponMoney = 0;
            // return false;
        }
        if (couponIdArr.length > 1) {
            $.toast("您只能使用一个优惠券", "text");
            return false;
        }
        $("#couponShow").html(couponCss);
        // 将金额也变掉
        debugger;
        var rpayMoney = parseFloat(paymentMoney) + parseFloat(couponMoney) - parseFloat(balanceMoney);

        if (rpayMoney <= 0) {
            // 重新选择支付方式,刷新页面吧
            $.toast("您的优惠券无法使用,请联系管理员", "text");
            window.location.href = "/phone/detail?memberId=" + memberId + "&orderId=" + orderId;
        }
        if ($("#balanceMoney").is(":checked") && rpayMoney <= 0) {
            // 重新选择支付方式,刷新页面吧
            $.toast("请重新选择优惠卷及余额的组合方式", "text");
            window.location.href = "/phone/detail?memberId=" + memberId + "&orderId=" + orderId;
        }
        $("#payMoney").val(rpayMoney);
        $("#payMent").html("合计：￥" + rpayMoney);

        $.closePopup();
    };

    function goAddressAndType(){
        // 正常
        if (type == "1") {
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+type;
        }
        // 临时订单
        if (type == "2"){
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+type+
                "&orderItemJson="+encodeURI(orderItemJson);
        }
        // 已生成订单
        if (type == "3"){
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+type+
                "&orderId="+orderId;
        }
    }
</script>
</html>

